from typing import Annotated

from fastapi import Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlmodel import SQLModel, Session

from config.setting import MYSQL_URL


class Database:
    def __init__(self):
        self.engine = create_engine(MYSQL_URL)
        self.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=self.engine)


mysql_db = Database()
Base = declarative_base()


def create_db_and_tables():
    SQLModel.metadata.create_all(mysql_db.engine)


def get_session():
    session = mysql_db.SessionLocal()
    try:
        yield session
    finally:
        session.close()


SessionDep = Annotated[Session, Depends(get_session)]
